package com.example.android.ui.searchHB;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.android.R;
import com.example.android.bean.SearchHB;
import com.example.android.ui.base.BaseActivity;
import com.example.android.ui.hb.detail.HBDetailActivity;
import com.example.android.ui.searchHB.Adapter.SearchHBRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

public class SearchHBActivity extends BaseActivity implements View.OnClickListener, SearchHBContract.ISearchHBView, SearchHBRecyclerViewAdapter.OnItemClickListener {
    private ImageView back;
    private Button search;
    private EditText value;
    private SearchHBPersenter searchHBPersenter;
    private SearchHBRecyclerViewAdapter searchHBRecyclerViewAdapter;

    @Override
    protected void initViews() {
        searchHBPersenter = new SearchHBPersenter(this);
        back = find(R.id.search_hb_back);
        search = find(R.id.search_hb_button);
        value = find(R.id.search_hb_edit);
        RecyclerView recyclerView = find(R.id.search_hb_recycler);
        List<SearchHB> searchHBList = new ArrayList<>();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        searchHBRecyclerViewAdapter = new SearchHBRecyclerViewAdapter(recyclerView,this,searchHBList);
        searchHBRecyclerViewAdapter.setOnItemClickListener(this);
        recyclerView.setAdapter(searchHBRecyclerViewAdapter);
        //点击事件
        back.setOnClickListener(this);
        search.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_hb;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.search_hb_back:
                finish();
                break;
            case R.id.search_hb_button:
                searchHBPersenter.getHBBasisList("hbName", value.getText().toString(), 0, 99999);
                break;
            default:
                break;
        }
    }

    @Override
    public void getHBBasisListSuccess(List<SearchHB> searchHBList) {
        searchHBRecyclerViewAdapter.setSearchHB(searchHBList);
    }

    @Override
    public void getHBBasisListError(Throwable throwable) {
        Log.e("SearchHBActivity",throwable.toString());
    }

    @Override
    public void onItemClick(SearchHB searchHB) {
        Intent intent = new Intent(this, HBDetailActivity.class);
        intent.putExtra("hbId",searchHB.getHbId());
        startActivity(intent);
    }
}
